package com.geek.novel.activity

import android.os.Bundle
import android.os.Message
import android.util.Log
import android.view.View
import com.geek.novel.R
import com.geek.novel.activity.auth.UserInfoActivity
import com.geek.novel.activity.base.TitleBarActivity
import com.geek.novel.bo.AppContext
import com.geek.novel.common.Constant
import com.geek.novel.dialog.ConfirmDialog
import com.geek.novel.services.ImageService
import com.geek.novel.services.manager.VersionUpdateManager
import com.geek.novel.utils.*
import io.realm.Realm
import kotlinx.android.synthetic.main.activity_settings.*
import java.io.File
import java.util.*

/**
 * 设置页面
 */
class SettingsActivity: TitleBarActivity() {

	override fun pageName(): String{
		return AppContext.getContext().getString(R.string.title_bar_page_setting)
	}

	/**更新的管理器*/
	private lateinit var versionUpdateManager: VersionUpdateManager

	/**确认框*/
	private lateinit var confirmDialog: ConfirmDialog

	//图片的服务类
	private lateinit var imageService: ImageService
	
	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		setContentView(R.layout.activity_settings)

		versionUpdateManager = VersionUpdateManager.getInstance(this)
		//图片服务
		imageService = ImageService(this)
		confirmDialog = ConfirmDialog(this)
	}

	/**附加控件*/
	override fun attachViewChild() {
		ly_setting_userinfo.setOnClickListener(this)
		ly_setting_typeface.setOnClickListener(this)
		ly_setting_user_protocol.setOnClickListener(this)
		ly_setting_app_info.setOnClickListener(this)
		ly_setting_version_info.setOnClickListener(this)
		ly_setting_logout.setOnClickListener(this)
		ly_setting_upload_log.setOnClickListener(this)

		//设置版本号
		tv_check_version.text = Constant.versionName

		//具备管理员权限，添加测试页面入口
		if(SecurityUtil.hasRoleAdmin()){
			ly_setting_test_page.visibility = View.VISIBLE
			ly_setting_test_page.setOnClickListener(this)
		}
	}

	override fun onResume() {
		super.onResume()
		//处理选项权限
		this.handleSettingPermission()
	}

	/**更新功能相关*/
	private fun checkUpdate(){
		versionUpdateManager.checkUpdate()
	}

	/**处理选项权限，没有登录不会出现一些按钮*/
	private fun handleSettingPermission(){
		val array = arrayOf(ly_setting_userinfo, ly_setting_logout)
		val user = SecurityUtil.getPrincipal()
		if(user == null){
			array.forEach { it.visibility = View.GONE }
		}else{
			array.forEach { it.visibility = View.VISIBLE }
		}
	}


	/**确认是否上传日志文件*/
	private fun confirmUploadLogFile(){
		//获取本地日志文件
		val path = LogCatUtil.getLogFile(Log.ERROR)
		val file = File(path)
		val size = file.length().toFloat() /1000 /1000

		confirmDialog.openDialog("确认上传本地数据文件吗？日志文件路径：${path}, 大小：${size}M", "上传") {
			this.uploadLogFile()
			confirmDialog.dismiss()
		}
	}

	/**
	 * 上传本地的日志文件
	 */
	private fun uploadLogFile(){
		//获取本地日志文件
		val path = LogCatUtil.getLogFile(Log.ERROR)
		val file = File(path)

		//获取当前用户的手机号或者用设备号
		val deviceId = CommUtil.deviceId

		//路径拼接上当前日期
		val date = DateUtil.getCurrentDate()
		val filePath = "$date/$deviceId"

		imageService.getFileService().uploadFile(file, "logs", null, "log-error", filePath)
	}

	/**消息处理*/
	override fun handleMessage(msg: Message) {
		when(msg.what){
			//文件上传完毕
			Constant.handler_event_upload_file_finish->{
				ToastUtils.toast("文件上传完毕")
			}
		}
	}

	override fun clickEvent(id: Int, v: View) {
		when(id){
			//个人资料
			R.id.ly_setting_userinfo->{
				startActivityByAnim(this@SettingsActivity, UserInfoActivity::class.java)
			}
			//字体设置
			R.id.ly_setting_typeface->{
				startActivityByAnim(this@SettingsActivity, TypefaceActivity::class.java)
			}
			//服务协议
			R.id.ly_setting_user_protocol->{
				ToastUtils.toast("服务协议-暂未开放")
			}
			//应用简介
			R.id.ly_setting_app_info->{
				ToastUtils.toast("应用简介-暂未开放")
			}
			//检查更新
			R.id.ly_setting_version_info->{
				this.checkUpdate()
			}
			//上传日志
			R.id.ly_setting_upload_log->{
				this.confirmUploadLogFile()
			}
			//测试页面
			R.id.ly_setting_test_page->{
				startActivityByAnim(this@SettingsActivity, TestPageActivity::class.java)
			}
			//退出登录
			R.id.ly_setting_logout->{
				SecurityUtil.logout()
				ToastUtils.toast("您已退出登录！")
				finish()
			}
		}
	}

}